﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web;
using VioletDevelopmentLaboratory.Net.Web.Component;

public partial class vboard_component_board_template_default_view_sub_button : BoardSubPartBase
{
	private BoardThread CurrentThread;

	private String getListUrl()
	{
		return BoardParameters.Part != BoardPart.List ?
			BoardUtil.GetBoardUrl(BoardPart.List, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0) :
			BoardUtil.GetBoardUrl(BoardPart.List, 0, "", "", "", 0, 0);
	}

	private String getThreadWriteUrl(out Boolean visible)
	{
		Boolean doWrite = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || base.CurrentUserInfo.IsAuthorizedBy(base.CurrentBoard, BoardCommand.ThreadWrite);

		if (doWrite)
		{
			visible = true;
			return BoardUtil.GetBoardUrl(BoardPart.Form_Add, BoardParameters.PageIndex, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0);
		}

		visible = false;
		return String.Empty;
	}

	private String getThreadReplyWriteUrl(out Boolean visible)
	{
		Boolean doReply = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || base.CurrentUserInfo.IsAuthorizedBy(base.CurrentBoard, BoardCommand.ThreadReplyWrite);

		if (base.CurrentBoard.UseReply && !this.CurrentThread.IsDeleted && !this.CurrentThread.IsBlocked && !this.CurrentThread.IsClosed && !this.CurrentThread.IsAnno && doReply)
		{
			visible = true;
			return BoardUtil.GetBoardUrl(BoardPart.Form_Add, BoardParameters.PageIndex, BoardParameters.CategoryID, BoardParameters.ThreadID, BoardParameters.SearchText, 0, 0);
		}

		visible = false;
		return String.Empty;
	}

	private String getThreadModifyUrl(out Boolean visible)
	{
		Boolean doModify = base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || (base.CurrentUserInfo.ProviderUserKey == this.CurrentThread.ProviderUserKey);
		if (!this.CurrentThread.IsDeleted && !this.CurrentThread.IsBlocked && doModify)
		{
			visible = true;
			return BoardUtil.GetBoardUrl(BoardPart.Form_Modify, BoardParameters.PageIndex, BoardParameters.CategoryID, BoardParameters.ThreadID, BoardParameters.SearchText, 0, 0);
		}

		visible = false;
		return String.Empty;
	}

	private Boolean threadDeleteButtonVisible()
	{
		if (this.CurrentThread.IsDeleted || this.CurrentThread.IsBlocked || !(base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard) || (base.CurrentUserInfo.ProviderUserKey == this.CurrentThread.ProviderUserKey)))
			return false;

		return true;
	}



	protected void Page_Init(Object sender, EventArgs e)
	{
		this.CurrentThread = (BoardThread)this.Page.Items["CurrentThread"];
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		//목록 링크
		ListHyperLink.NavigateUrl = this.getListUrl();

		Boolean linkVisible = true;

		//글쓰기 링크
		WriteHyperLink.NavigateUrl = this.getThreadWriteUrl(out linkVisible);
		WriteHyperLink.Visible = linkVisible;

		//답글쓰기 링크
		ReplyHyperLink.NavigateUrl = this.getThreadReplyWriteUrl(out linkVisible);
		ReplyHyperLink.Visible = linkVisible;

		//글변경 링크
		ModifyHyperLink.NavigateUrl = this.getThreadModifyUrl(out linkVisible);
		ModifyHyperLink.Visible = linkVisible;

		//삭제 버튼
		DeleteButton.Visible = this.threadDeleteButtonVisible();

		//관리자 버튼
		AdminButtonPanel.Visible = !this.CurrentThread.IsDeleted && (base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard));
	}

	protected void ThreadBlockButton_Click(Object sender, EventArgs e)
	{
		if (base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard))
		{
			switch (this.CurrentThread.SetBlock())
			{
				case BoardThreadSetBlockResult.Success:
					VUtility.GoToMessagePage(Page,
						VUtility.GetVWebGlobalResource("Board_Do_ThreadBlock_Success"),
						BoardUtil.GetBoardUrl(BoardPart.View, BoardParameters.PageIndex, BoardParameters.CategoryID, BoardParameters.ThreadID, BoardParameters.SearchText, 0, 0), true, 3);
					break;
				case BoardThreadSetBlockResult.InvalidThread:
					ScriptManager.RegisterStartupScript(Page, this.GetType(), "Result",
						VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadBlock_Failure"), true), true);
					break;
			}
		}
		else
		{
			VUtility.GoToMessagePage(Page, VUtility.GetAlertScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadBlock_Rejection")), this.getListUrl(), true, 3);
		}
	}

	protected void ThreadClearButton_Click(Object sender, EventArgs e)
	{
		if (base.CurrentUserInfo.IsAdministrator || base.CurrentUserInfo.IsSysop(base.CurrentBoard))
		{
			switch (this.CurrentThread.SetClear())
			{
				case BoardThreadSetClearResult.Success:
					VUtility.GoToMessagePage(Page,
						VUtility.GetVWebGlobalResource("Board_Do_ThreadClear_Success"),
						BoardUtil.GetBoardUrl(BoardPart.View, BoardParameters.PageIndex, BoardParameters.CategoryID, BoardParameters.ThreadID, BoardParameters.SearchText, 0, 0), true, 3);
					break;
				case BoardThreadSetClearResult.InvalidThread:
					VUtility.GoToMessagePage(Page, "Result",
						VUtility.GetMessageScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadClear_Failure"), true), true, 3);
					break;
			}
		}
		else
		{
			VUtility.GoToMessagePage(Page, VUtility.GetAlertScript(VUtility.GetVWebGlobalResource("Board_Do_ThreadClear_Rejection")), this.getListUrl(), true, 3);
		}
	}

	protected void DeleteButton_Click(Object sender, EventArgs e)
	{
		if (base.CurrentUserInfo.IsAdministrator ||
			base.CurrentUserInfo.IsSysop(base.CurrentBoard) ||
			(base.CurrentUserInfo.Name == this.CurrentThread.UserName && base.CurrentUserInfo.ProviderUserKey == this.CurrentThread.ProviderUserKey))
		{
			BoardThreadDeleteResult result = this.CurrentThread.Remove();

			switch (result)
			{
				case BoardThreadDeleteResult.Success:
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadDelete_Success"),
						BoardUtil.GetBoardUrl(BoardPart.List, 0, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0),
						true, 3);
					break;
				case BoardThreadDeleteResult.InvalidThread:
					VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadDelete_Failure"),
						BoardUtil.GetBoardUrl(BoardPart.List, 0, BoardParameters.CategoryID, "", BoardParameters.SearchText, 0, 0),
						true, 3);
					break;
			}
		}
		else
		{
			VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board_Do_ThreadDelete_Rejection"), this.getListUrl(), true, 3);
		}
	}
}
